import { Tooltip } from 'antd';
import pageStyles from '@/common/pageLayout.less';
import { getColorTextLoadMap } from '@/utils';

/**判断值是否有两位小数点，没有则自动补零*/
export const formatDecimal = (val: any) => {
  // 不存在的话就返回'--'
  if (!val) return '--';
  // 将输入值转为数字，以确保处理的是数值类型
  const num = Number(val);
  // 如果转换后的数值是NaN，则直接返回原始值
  if (isNaN(num)) return '--';
  // 使用toFixed方法确保小数点后有两位，然后再转回字符串
  return num.toFixed(2) + '';
};

/** columns内容渲染的方法【非字典】 */
export const ColumnsRenderFun = ({ isTooltip = true, text, textStyle = {}, event = undefined, record = {}, type = 'Text' }: Record<string, any>) => {
  if (type == 'LoapMap') {
    // 字典
    const { colorText, color } = getColorTextLoadMap(text);
    return (
      <>
        <Tooltip title={isTooltip ? text : undefined}>
          <span className={pageStyles['over-flow-hidden']} onClick={event ? () => event(record) : undefined} style={{ color, ...textStyle }}>
            {colorText == null || colorText == undefined || colorText == '' ? '--' : colorText}
          </span>
        </Tooltip>
      </>
    );
  } else if (type == 'Text') {
    return (
      <>
        <Tooltip title={isTooltip ? text : undefined}>
          <span className={pageStyles['over-flow-hidden']} onClick={event ? () => event(record) : undefined} style={textStyle}>
            {text == null || text == undefined || text == '' ? '--' : text}
          </span>
        </Tooltip>
      </>
    );
  }
};

/** columns内容渲染的方法【字典】 */
export const DictionaryRenderFun = ({ isTooltip = false, text, textStyle = {}, event = undefined, record = {} }: Record<string, any>) => {
  const { colorText, color } = getColorTextLoadMap(text);
  return (
    <>
      <Tooltip title={isTooltip ? text : undefined}>
        <span className={pageStyles['over-flow-hidden']} onClick={event ? () => event(record) : undefined} style={{ color, ...textStyle }}>
          {colorText == null || colorText == undefined || colorText == '' ? '--' : colorText}
        </span>
      </Tooltip>
    </>
  );
};

/** 处理公共管理组件 下拉框 数据 ,返回 {value: '', name: ''} */
export const getSelectDataCommon = (selectData: any[], name: any, value: any) => {
  let newData = selectData?.map((item) => {
    return {
      name: item?.[`${name}`],
      value: item?.[`${value}`],
    };
  });
  return newData;
};

/**
 * @renderOverflowText
 * @description 处理文字溢出
 * @param text 值
 * @param isShowTooltip 是否溢出隐藏并且加Tooltip
 * @returns
 */
export const renderOverflowText = (text: string | number | null, isShowTooltip = false) => {
  const index = ['1', '0'].indexOf(String(text) || '');
  const { colorText, color } = getColorTextLoadMap(String(text));
  const info = {
    word: colorText == 'undefined' || colorText == 'null' || !colorText ? '--' : colorText,
    color: color,
  };
  if (index != -1) {
    info.word = index == 1 ? '是' : '否';
    info.color = index == 1 ? '#409eff' : 'red';
  }
  if (isShowTooltip && info.word != '--') {
    return (
      <Tooltip title={info.word}>
        <span className={pageStyles['over-flow-hidden']} style={{ color: info.color }}>
          {info.word}
        </span>
      </Tooltip>
    );
  } else {
    return <span style={{ color: info.color }}>{info.word}</span>;
  }
};
